import fs from 'fs';
import { getParams, getDescription, getDisplayName, getDeprecatedInfo, getKey, parse } from './reactComponentInfoUtils.js';
import jsxToAst from './jsxToAst.js';
import extractReactComponents from './extractReactComponents.js';
import jsdocToJson from './jsdocToJson.js';
import formatJSDocComments from './formatJSDocComments.js';

/**
 * 从JS文件路径数组提取React组件信息
 * @param {string[]} filePaths JS文件绝对路径数组
 * @returns {Promise<Array>} React组件信息数组
 */
async function getReactComponentsFromFiles(filePaths) {
  const componentInfos = [];
  
  for (const filePath of filePaths) {
    try {
      const code = fs.readFileSync(filePath, 'utf-8');
      const ast = jsxToAst(code);
      const componentMap = extractReactComponents(ast);
      
      for (const [componentName, jsDoc] of componentMap) {
        const jsonDoc = jsdocToJson(formatJSDocComments(jsDoc));
        componentInfos.push({
          name: componentName,
          absolutePath: filePath,
          jsDoc: jsonDoc,
        });
      }
    } catch (error) {
      console.error(`Error processing file ${filePath}:`, error);
    }
  }
  
  return componentInfos;
}

export { getReactComponentsFromFiles };